# @Time: 2023/10/12 16:46
# @Auther: MHC
# @File: aliyun.py
# @Description: 阿里云的AI大模型体验

import random
from http import HTTPStatus
from dashscope import Generation
import dashscope
# 此处使用您自己的api_key值
dashscope.api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxx"


def response(text):
    messages = [
        {'role': 'user', 'content': text}]
    gen = Generation()
    response = gen.call(
        'qwen-14b-chat',
        messages=messages,
        seed=random.randint(1, 10000),  # set the random seed, optional, default to 1234 if not set
        result_format='message',  # set the result to be "message" format.
    )
    if response.status_code == HTTPStatus.OK:
        # 获取output中的choices中的message
        message = response.output.choices[0].message["content"]
        print(message)
        return message
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))


if __name__ == '__main__':
    response('你是谁？')
